#include "BlockQueue.hpp"
#include "Task.hpp"

void* Consumer(void* args)
{
    BlockQueue<Task>* bq = static_cast<BlockQueue<Task>*>(args);
    while(true)
    {
        // 消费
        Task t = bq->pop();
        // t.GetResult();
        // t.run();
        t();
        std::cout << "处理任务: " << t.GetTask() << "运算结果是: " << t.GetResult() << std::endl;
        // sleep(1);
    }
}

void* Product(void* args)
{
    int len = opers.size();
    BlockQueue<Task>* bq = static_cast<BlockQueue<Task>*>(args);
    // int x = 1;
    // int y = 1;
    while(true)
    {
        int data1 = rand() % 10 + 1;
        int data2 = rand() % 5 + 1;
        char op = opers[rand() % len];

        // sleep(2);
        // 生产
        Task t(data1, data2, op);
        bq->push(t);
        std::cout << "生产了一个任务: " << t.GetTask() << std::endl;
        sleep(1);
    }
}

int main()
{
    srand(time(nullptr));
    BlockQueue<Task>* bq = new BlockQueue<Task>;
    pthread_t c,p;
    pthread_create(&c, nullptr, Consumer, bq);
    pthread_create(&p, nullptr, Product, bq);

    pthread_join(c,nullptr);
    pthread_join(p,nullptr);

    delete bq;
    return 0;
}